#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <math.h>
#include <execinfo.h>
#include <signal.h>
#include "config.h"
#include "build_link.h"
#include "radar_protocol.h"
#include "fusion.h"
#include "log.h"
#include <curl/curl.h>

void crash_handler(int sig)
{
    void *buffer[12];
    int nptrs = backtrace(buffer, 10);

    if (sig == SIGINT) {
        close_all_socket();
    }
    else {
        fprintf(stderr, "Error: signal %d:\n", sig);
        backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
    }
    exit(1);
}

int main()
{
    signal(SIGSEGV, crash_handler);  // 捕获段错误
    signal(SIGABRT, crash_handler);  // 捕获 abort 信号
    signal(SIGINT, crash_handler);   // 捕获中断信号
    /* 初始化全局 libcurl */
    curl_global_init(CURL_GLOBAL_DEFAULT);
    // 读取配置文件
    read_config(CONFIG_FILE_PATH, &g_config);
    print_config(&g_config);
    init_communication();
    start_fusion_server();

    while (1) {
        sleep(1);
    }
    stop_fusion_server();
    curl_global_cleanup();

    return 0;
}